package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.gameapplication.BaseObject;
import com.example.gameapplication.R;

public class Bullet extends BaseObject {
    private Bitmap mBullet;
    private int currentmBulletId;

    public void setLv(int lv) {
        if (getLv() != lv) {
            this.lv=lv;
            initBitmap();
        }
    }

    public Bullet(Resources resources, Paint paint) {
        super(resources, paint);
    }

    @Override
    public void initBitmap() {

        //子弹外形
        switch (getLv()) {
            case 1:
                if (currentmBulletId==R.drawable.my_bullet_blue)return;
                currentmBulletId=R.drawable.my_bullet_blue;
                break;
            case 2:
                if (currentmBulletId==R.drawable.my_bullet_purple)return;
                currentmBulletId=R.drawable.my_bullet_purple;
                break;
            case 3:
                if (currentmBulletId==R.drawable.my_bullet_red)return;
                currentmBulletId=R.drawable.my_bullet_red;
                break;
            case 4:
                if (currentmBulletId==R.drawable.boss_bullet_hellfire_yellow)return;
                currentmBulletId=R.drawable.boss_bullet_hellfire_yellow;
                break;
            default:
                if (currentmBulletId==R.drawable.boss_bullet_hellfire_red)return;
                currentmBulletId=R.drawable.boss_bullet_hellfire_red;
                break;
        }
        if (mBullet != null && !mBullet.isRecycled()) mBullet.recycle();
        mBullet = BitmapFactory.decodeResource(resources,currentmBulletId);
        object_width = mBullet.getWidth();
        object_height = mBullet.getHeight();
    }

    public boolean isAlive() {
        return super.isAlive();
    }

    @Override
    public void setSpeed(int speed) {
        this.speed = speed;
    }


    @Override
    public void drawSelf(Canvas canvas) {//子弹的飞行逻辑
        if (!isAlive()) return;
        setSeat(midx, midy);
        //画子弹
        canvas.save();
        canvas.clipRect(leftx, lefty, rightx, righty);
        canvas.drawBitmap(mBullet, leftx, lefty, paint);
        canvas.restore();
        logic();

    }

    public void logic() {//子弹的逻辑，产生之后，x不变，y轴变化
        if (midy < 0) {//离开屏幕
            setAlive(false);
            return;
        }
        if (isAlive())//子弹击中目标，则在离开屏幕之前就被销毁了
            midy = midy - speed;
    }

    @Override
    public void dataDestroyed() {
        if (mBullet!=null&&!mBullet.isRecycled()) {
            mBullet.recycle();
        }
    }
}
